package com.zjzywl.smoke.repository;

import com.zjzywl.smoke.entity.Alarm;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface AlarmRepository extends JpaRepository<Alarm, Integer>, AlarmCustomRepository {

	List<Alarm> findByBeginTimeAfter(Date beginTime);

	List<Alarm> findByBeginTimeBetweenOrderByBeginTime(Date beginTime, Date endTime);

	@Query("select u from Alarm u where u.beginTime>=:beginTime and u.beginTime <=:endTime and device.devCode=:devcode")
	List<Alarm> findByBeginTimeAndDevCode(@Param("beginTime") Date beginTime, @Param("endTime") Date endTime,
			@Param("devcode") String devcode);

	@Query("select u from Alarm u where u.beginTime>=:beginTime and u.beginTime <=:endTime and device.devCode=:devcode "
			+ " and alarmType.alarmCode=:alarmTypeCode")
	List<Alarm> findByBeginTimeAndDevCodeAndAlarmTypeCode(@Param("beginTime") Date beginTime,
			@Param("endTime") Date endTime, @Param("devcode") String devcode,
			@Param("alarmTypeCode") String alarmTypeCode);

	@Query("select u from Alarm u where u.beginTime>=:beginTime and u.beginTime <=:endTime "
			+ " and alarmType.alarmCode=:alarmTypeCode")
	List<Alarm> findByBeginTimeAndAlarmTypeCode(@Param("beginTime") Date beginTime, @Param("endTime") Date endTime,
			@Param("alarmTypeCode") String alarmTypeCode);

	// 获取未结束报警
	List<Alarm> findByEndTimeIsNull();

	// 获取报警对应状态，当ds为0,获取未处理报警
	List<Alarm> findByDisposeStatus(Integer ds);

	// @Query("select u from Alarm u where u.disposeStatus=:ds and
	// u.alarmType.alarmCode= :alarmCode")
	// List<Alarm> findByDisposeStatusAndAlarmType(@Param("ds") Integer
	// ds,@Param("alarmCode") String alarmCode);

	@Modifying
	@Query("update Alarm u set u.disposeStatus=:disposeStatus,u.disposePeople=:name,u.disposeContent=:disposeContent where u.disposeStatus=0")
	void updateAllDisposeStatus(@Param("disposeStatus") Integer disposeStatus, @Param("name") String name,
			@Param("disposeContent") String disposeContent);

	@Query("select u from Alarm u where u.beginTime>=:beginTime and u.beginTime <=:endTime and u.device.address=:address")
	List<Alarm> findByBeginTimeAndAddress(@Param("beginTime") Date beginTime, @Param("endTime") Date endTime,
			@Param("address") String address);

	@Query("select count(*) from Alarm u where u.beginTime>=:beginTime and u.beginTime <=:endTime AND alarmType.alarmTypeId=:alarmTypeId and u.disposePeople=:disposePeople")
	int findByBeginTime(@Param("beginTime") Date bTime, @Param("endTime") Date eTime,
			@Param("alarmTypeId") int alarmTypeId, @Param("disposePeople") String disposePeople);

	@Query("select count(*) from Alarm u where u.beginTime>=:beginTime and u.beginTime <=:endTime AND alarmType.alarmTypeId=:alarmTypeId and u.disposePeople=:disposePeople")
	int findByBegin(@Param("beginTime") Date bTime, @Param("endTime") Date eTime,
			@Param("alarmTypeId") int alarmTypeId);
	// @Query("select u from Alarm u where to_days(beginTime) = to_days(now())AND
	// alarmType.alarmTypeId=:alarmTypeId")
	// List<Alarm>findByBeginTime(@Param("alarmTypeId") int alarmTypeId);

	@Query("select u.timeDifference from Alarm u where u.alarmType.alarmCode=:alarmCode and u.timeDifference>0 and u.beginTime>=:beginTime and u.beginTime <=:endTime and u.disposePeople=:disposePeople")
	List findByTimeDifference(@Param("beginTime") Date bTime, @Param("endTime") Date eTime,
			@Param("alarmCode") String alarmCode, @Param("disposePeople") String disposePeople);

	@Query("select u from Alarm u where u.alarmType.alarmCode=:alarmCode and u.alarmReasons=:alarmReasons and u.beginTime>=:beginTime and u.beginTime <=:endTime and u.disposePeople=:disposePeople")
	List getAlarmReason(@Param("beginTime") Date bTime, @Param("endTime") Date eTime,
			@Param("alarmCode") String alarmCode, @Param("alarmReasons") String alarmReasons,
			@Param("disposePeople") String disposePeople);

	@Query("select u from Alarm u where u.alarmType.alarmCode=:alarmCode and u.beginTime>=:beginTime and u.beginTime <=:endTime and u.disposePeople=:disposePeople")
	List getAlarmReasons(@Param("beginTime") Date bTime, @Param("endTime") Date eTime,
			@Param("alarmCode") String alarmCode, @Param("disposePeople") String disposePeople);

	@Query("select u from Alarm u where u.alarmType.alarmCode=:alarmCode  and u.beginTime>=:beginTime and u.beginTime <=:endTime and u.disposePeople=:disposePeople")
	List getAlarmReason(@Param("beginTime") Date bTime, @Param("endTime") Date eTime,
			@Param("alarmCode") String alarmCode, @Param("disposePeople") String disposePeople);

	@Query("select u.alarmReasons from Alarm u where u.disposePeople=:disposePeople and u.alarmReasons is not null  group by u.alarmReasons")
	List getAlarmReasonByuser(@Param("disposePeople") String disposePeople);

	@Transactional
	@Query(value = "delete from Alarm where deviceid=:deviceid ", nativeQuery = true)
	@Modifying
	void deleteByDeviceId(@Param("deviceid") Integer deviceid);

}
